package com.example.amazlinglistview.task;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;
import android.view.View;

import com.example.amazlinglistview.MainActivity;
import com.example.amazlinglistview.domain.Cars;
import com.example.amazlinglistview.domain.SubSeries;
import com.example.amazlinglistview.utils.NetUtils;

public class CarSeriesTask extends AsyncTask<String, Void, Boolean> {

	private static final String TAG = "CarSeriesTask";
	private MainActivity mContext;

	public CarSeriesTask(MainActivity context) {
		this.mContext = context;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		if (mContext.mProgressbar.getVisibility() != View.VISIBLE) {
			mContext.mProgressbar.setVisibility(View.VISIBLE);
		}
	}

	@Override
	protected Boolean doInBackground(String... params) {
		boolean result = false;
		try {
			result = (Boolean) parseSeriesJson(NetUtils.getJson(params[0]));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/** 解析JSON */
	private Boolean parseSeriesJson(String json) {
		// Log.e(TAG, "json = "+json);
		if (null != json) {
			try {
				JSONObject seriesInfoObj = new JSONObject(json);
				List<SubSeries> subSeries = new ArrayList<SubSeries>();
				mContext.mSeriesInfo
						.setMsgId(seriesInfoObj.isNull("msgId") == false ? seriesInfoObj
								.getString("msgId") : "");
				mContext.mSeriesInfo
						.setImage(seriesInfoObj.isNull("image") == false ? seriesInfoObj
								.getString("image") : "");
				mContext.mSeriesInfo.setImageNum(seriesInfoObj
						.isNull("imageNum") == false ? seriesInfoObj
						.getInt("imageNum") : 0);
				mContext.mSeriesInfo
						.setName(seriesInfoObj.isNull("name") == false ? seriesInfoObj
								.getString("name") : "");
				mContext.mSeriesInfo.setPriceByDealer(seriesInfoObj
						.isNull("priceByDealer") == false ? seriesInfoObj
						.getString("priceByDealer") : "");
				mContext.mSeriesInfo.setPriceByVendor(seriesInfoObj
						.isNull("priceByVendor") == false ? seriesInfoObj
						.getString("priceByVendor") : "");
				mContext.mSeriesInfo.setSubSeriesNum(seriesInfoObj
						.isNull("subSeriesNum") == false ? seriesInfoObj
						.getInt("subSeriesNum") : 0);
				mContext.mSeriesInfo
						.setBrandName(seriesInfoObj.isNull("bname") == false ? seriesInfoObj
								.getString("bname") : "");
				mContext.mSeriesInfo.setLevelName(seriesInfoObj
						.isNull("level_name") == false ? seriesInfoObj
						.getString("level_name") : "");
				mContext.mSeriesInfo
						.setClubId(seriesInfoObj.isNull("clubid") == false ? seriesInfoObj
								.getString("clubid") : "0");
				for (int i = 0; i < seriesInfoObj.getJSONArray("subSeries")
						.length(); i++) {
					JSONObject subSeriesObject = seriesInfoObj.getJSONArray(
							"subSeries").getJSONObject(i);
					SubSeries subSeriesItem = new SubSeries();
					List<Cars> cars = new ArrayList<Cars>();
					subSeriesItem.setSeriesName(subSeriesObject
							.isNull("seriesName") == false ? subSeriesObject
							.getString("seriesName") : "");
					subSeriesItem
							.setCarNum(subSeriesObject.isNull("carNum") == false ? subSeriesObject
									.getInt("carNum") : 0);
					subSeriesItem
							.setSeriesId(subSeriesObject.isNull("serieId") == false ? subSeriesObject
									.getString("serieId") : "");
					for (int j = 0; j < subSeriesObject.getJSONArray("cars")
							.length(); j++) {
						JSONObject carsObject = subSeriesObject.getJSONArray(
								"cars").getJSONObject(j);
						Cars carsItem = new Cars();
						carsItem.setId(carsObject.isNull("id") == false ? carsObject
								.getString("id") : "");
						carsItem.setName(carsObject.isNull("name") == false ? carsObject
								.getString("name") : "");
						carsItem.setPrice(carsObject.isNull("price") == false ? carsObject
								.getString("price") : "");
						carsItem.setGuidePrice(carsObject.isNull("guidePrice") == false ? carsObject
								.getString("guidePrice") : "");

						carsItem.setEngine(carsObject.isNull("engine") == false ? carsObject
								.getString("engine") : "");
						carsItem.setTransmissionDes(carsObject
								.isNull("transmissionDes") == false ? carsObject
								.getString("transmissionDes") : "");
						cars.add(carsItem);
					}
					subSeriesItem.setCars(cars);
					subSeries.add(subSeriesItem);
					mContext.mSeriesInfo.setSubSeries(subSeries);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return true;
		} else {
			return false;
		}
	}

	@Override
	protected void onPostExecute(Boolean result) {
		super.onPostExecute(result);
		if (result) {
			mContext.mHandler.sendEmptyMessage(1);
		} else {
			mContext.mHandler.sendEmptyMessage(-1);
		}
	}

}
